<?php

require_once("../../connection.php");

require_once("../../article_article_category/to/article_article_category_to.php");
require_once("../../article_article_category/dao/article_article_category_dao.php");


class Mysql_article_article_category_dao implements Article_article_category_dao {

	public function create($article_article_category_to) {
		try {
			$link = Connection::singleton()->transaction_begin();
				
			$article_number = $article_article_category_to->get_article_number();
			$article_category_number = $article_article_category_to->get_article_category_number();

			$result = mysql_query("insert into article_article_category	
									(article_number, article_category_number) values 
									('$article_number', '$article_category_number')",$link);
																	
			if (!$result || (mysql_affected_rows($link) != 1)) 
					throw new Exception(NOT_ROW_INSERTED);
			
			Connection::singleton()->transaction_commit($link);
			
			$article_article_category_to = new Article_article_category_to($article_number, $article_category_number);
			return $article_article_category_to;
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			Exception_handler::singleton()->handler($e);
		}
	}
	
	public function exist($article_number, $article_category_number){
		try {
			$link = Connection::singleton()->transaction_begin();
			
			$result = mysql_query("select * from article_article_category
									where article_number='$article_number' and
									article_category_number='$article_category_number'", $link);
									
			if (!$result || (mysql_num_rows($result) != 1)) {
				Connection::singleton()->transaction_commit($link);
				return false;								
			}

			Connection::singleton()->transaction_commit($link);
			return true;
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			Exception_handler::singleton()->handler($e);
		}
	}
	
	public function find_by_number_and_category_article($article_number, $article_category_number) {
		try {
			$link = Connection::singleton()->transaction_begin();
			
			$result = mysql_query("select * from article_article_category
									where article_number='$article_number' and
									article_category_number='$article_category_number'", $link);
									
			if (!$result || (mysql_num_rows($result) != 1))
				throw new Exception(NOT_ROW_FOUND);
			
			Connection::singleton()->transaction_commit($link);
			return mysql_fetch_assoc($result);
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($e);
			Exception_handler::singleton()->handler($e);
		}
	}
	
	public function update() {}
	
	public function remove($article_number, $article_category_number) {
		try {
			 $link = Connection::singleton()->transaction_begin();
			 
			 $result = mysql_query("delete from article_article_category
			 						where article_number='$article_number' and
									article_category_number='$article_category_number'", $link);
			 
			 if (!$result || (mysql_affected_rows($link) != 1))
			 	throw new Exception(NOT_ROW_DELETED);			
			
			Connection::singleton()->transaction_commit($link);
			
		}
		catch (Exception $e){
			Connection::singleton()->transaction_rollback($link);
			Exception_handler::singleton()->handler($e);
		}
	}
	
}
?>